<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import gdjdService from '@/api/gdjd'

// 入参
const dialogVisible = ref(false)
const multipleSelection = ref<any[]>([])
const openDialog = (rowData: any) => {
  multipleSelection.value = rowData
  dialogVisible.value = true
}

// 下载
const download = () => {
  const params = {
    jdbbbh: multipleSelection.value[0].jdbbbh,
  }
  gdjdService
    .downloadJdbb(params)
    .then((res: any) => {
      if (res.data.type === 'application/json') {
        const reader = new FileReader()
        reader.onload = () => {
          const jsonData =
            typeof reader.result === 'string' ? JSON.parse(reader.result) : reader.result
          ElMessage({ type: 'warning', message: jsonData.message })
        }
        reader.readAsText(res.data)
      } else {
        const downloadElement = document.createElement('a')
        downloadElement.href = window.URL.createObjectURL(res.data)
        downloadElement.download = '监督报表.docx'
        document.body.appendChild(downloadElement)
        downloadElement.click() //点击下载
        document.body.removeChild(downloadElement) //下载完成移除元素
      }
    })
    .catch((err) => {
      !err.isError && ElMessage({ type: 'warning', message: err.message })
    })
}

defineExpose({ openDialog })
</script>
<template>
  <el-dialog v-model="dialogVisible" title="监督报告" width="80%" :close-on-click-modal="false">
    <div class="dialog-box">
      <!--文件-->
      <!-- <wjzs ref="wjzsRef" wjlx="pdf" :url="url" /> -->
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="download">下载</el-button>
        <el-button type="primary" @click="dialogVisible = false">打印</el-button>
        <el-button type="default" @click="dialogVisible = false">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style lang="scss" scoped>
.dialog-box {
  width: 100%;
  height: 70vh;
}
</style>
